package com.cskaoyan.config.realm;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.StringUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Fyy
 * @since 2022/12/30 16:53
 */

@Component
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    MarketAdminMapper adminMapper;
    @Autowired
    MarketRoleMapper roleMapper;
    @Autowired
    MarketPermissionTypeMapper permissionMapper;
    @Autowired
    MarketUserMapper userMapper;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MarketToken marketToken = (MarketToken) authenticationToken;
        String username = marketToken.getUsername();
        String type = marketToken.getType();
        if ("admin".equals(type)) {
            MarketAdminExample example = new MarketAdminExample();
            if (!StringUtil.isEmpty(username)) {
                example.createCriteria().andUsernameEqualTo(username);
            }
            List<MarketAdmin> admins = adminMapper.selectByExample(example);
            if (admins.size() == 1) {
                MarketAdmin marketAdmin = admins.get(0);
                return new SimpleAuthenticationInfo(marketAdmin, marketAdmin.getPassword(), getName());
            }
        }else if("wx".equals(type)){
            MarketUserExample example = new MarketUserExample();
            if (!StringUtil.isEmpty(username)) {
                example.createCriteria().andUsernameEqualTo(username);
            }
            List<MarketUser> users = userMapper.selectByExample(example);
            if (users.size() == 1) {
                MarketUser marketUser = users.get(0);
                return new SimpleAuthenticationInfo(marketUser, marketUser.getPassword(), getName());
            }
        }
        return null;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获得principal信息
        MarketAdmin marketAdmin = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        //根据用户信息查询权限列表
        Integer[] roleIds = marketAdmin.getRoleIds();
        MarketPermissionTypeExample permissionExample = new MarketPermissionTypeExample();
        if (!ObjectUtils.isEmpty(roleIds)) {
            permissionExample.createCriteria().andTypeIn(Arrays.asList(roleIds));
        }
        List<MarketPermissionType> permissionList = permissionMapper.selectByExample(permissionExample);
        List<String> permList = new ArrayList<>();
        for (MarketPermissionType permission : permissionList) {
            if (permission.getApi() != null) {
                permList.add(permission.getApi());
            }
        }
        //获得管理员角色名
        MarketRoleExample example = new MarketRoleExample();
        if (!ObjectUtils.isEmpty(roleIds)) {
            example.createCriteria().andIdIn(Arrays.asList(roleIds));
        }
        List<MarketRole> roles = roleMapper.selectByExample(example);
        List<String> nameList = new ArrayList<>();
        for (MarketRole role : roles) {
            nameList.add(role.getName());
        }
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRoles(nameList);
        authorizationInfo.addStringPermissions(permList);
        return authorizationInfo;
    }


}
